package com.dirtylily.myblog.controller;

import com.dirtylily.myblog.pojo.Tag;
import com.dirtylily.myblog.pojo.Type;
import com.dirtylily.myblog.service.TagService;
import com.dirtylily.myblog.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class TagController {

    @Autowired
    private TagService tagService;

//    分页查询
    @GetMapping("/tags")
    public String list(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        //按照排序字段 倒序 排序
        String orderBy = "id asc";
        PageHelper.startPage(pageNum,5,orderBy);
        List<Tag> list = tagService.getAllTag();
        PageInfo<Tag> pageInfo = new PageInfo<Tag>(list);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/tags";
    }

    @GetMapping("/tags/input")
    public String input(Model model){
        model.addAttribute("tag", new Tag());
        return "admin/tag-input";
    }

    @PostMapping("/tags")
    public String save(@RequestParam String name, RedirectAttributes rd){
        List<Tag> allTag = tagService.getAllTag();
        int size = allTag.size();
        Tag tag;
        if(size==0){
             tag = new Tag(1l, name,
                    null);
        }else {

        Tag tag1 = allTag.get(size - 1);
         tag = new Tag(tag1.getId()+1, name,
                null);
        }

        if(tagService.getTagByName(name)!=null){
            rd.addFlashAttribute("msg","已经存在重复标签，请重新输入");
            return "redirect:/admin/tags";

        }
        int i = tagService.saveTag(tag);
        if(i!=0){
            rd.addFlashAttribute("msg","添加成功啦！");

        }
        else {
            rd.addFlashAttribute("msg","添加失败！");
        }
        return "redirect:/admin/tags";
    }


    @GetMapping("/tags/{id}/input")
    public String toUpdate(@PathVariable Long id,Model model){
        Tag tags = tagService.getTag(id);
        model.addAttribute("tag",tags);
        return "admin/tag-input";

    }



    @GetMapping("/tags/{id}/delete")
    public String delTags(@PathVariable Long id,RedirectAttributes rd){
        int i = tagService.delTag(id);
        if(i!=0){
            rd.addFlashAttribute("msg","删除成功啦！");

        }
        else {
            rd.addFlashAttribute("msg","删除失败！");
        }
        return "redirect:/admin/tags";

    }

    @PostMapping("/tags/{id}")
    public String update(@RequestParam String name, RedirectAttributes rd, @RequestParam Long id){

        if(tagService.getTagByName(name)!=null){
            rd.addFlashAttribute("msg","已经存在重复标签，请重新输入");
            return "redirect:/admin/tags";

        }
        Tag tag = tagService.getTag(id);
        tag.setName(name);
        int i = tagService.updateTag(tag);
        if(i!=0){
            rd.addFlashAttribute("msg","修改成功啦！");

        }
        else {
            rd.addFlashAttribute("msg","修改失败！");
        }
        return "redirect:/admin/tags";
    }
}
